package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import models.Company;
import models.Member;
import models.Product;
import models.Sale;
import models.Types;

import org.apache.commons.io.IOUtils;

import play.Play;
import play.Play.Mode;
import play.mvc.Controller;
import util.FileUtil;
import util.StringConvert;

/**
 * @author sun 填写出售信息
 */
public class SaleInfo extends Controller {

	
	public static void newSale() {

		String memberid = session.get("memberid");

		if (null == memberid || "".equals(memberid.trim())) {

			redirect("/login/index");
		}

		render();
	}

	/**
	 * 得到提示的参数
	 */
	public static void getInfo() {

		String args = params.get("q");

		if (null == args || args.trim().length() == 0) {

			return;
		}

		String queryString = "";
		try {
			queryString = StringConvert.unEscape(java.net.URLDecoder.decode(args, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		String type = params.get("type");

		if ("company".equals(type)) {// 厂商

			List<Company> fetch = Company.find("name like ?", "%" + queryString.trim() + "%").fetch();
			String json = parseCompleteJsonForCompany(fetch);
			renderJSON(json);

		} else if ("types".equals(type)) {// 产品类型

			List<Types> fetch = Types.find("name like ? ", "%" + queryString.trim() + "%").fetch();
			String json = parseCompleteJsonForTypes(fetch);
			renderJSON(json);

		} else if ("product".equals(type)) {// 产品名称

			List<Product> fetch = Product.find("name like ? ", "%" + queryString.trim() + "%").fetch();
			String json = parseCompleteJsonForProduct(fetch);
			renderJSON(json);
		}

	}

	/**
	 * 
	 * 保存销售信息
	 * 
	 * @param title
	 * @param company
	 * @param types
	 * @param product
	 * @param desc
	 * @param status
	 * @param price
	 * @param pic
	 * @param contract
	 * @param tel
	 * @param email
	 */
	public static void saveSaleInfo(String title, String company, String types, String product, String desc,
			String status, String price, File pic, String contract, String tel, String email) {
		String memberid = session.get("memberid");
		if (null == memberid || "".equals(memberid.trim())) {
			redirect("/login/index");
		}

		// ----------验证厂商 类型 产品信息  ----------
		Product targetProduct = saveOrUpdateCompanyTypeProduct(company, types, product);

		Member member = Member.find("id", Long.valueOf(memberid)).first();
		
		String picPath ="";
		if(null!=pic){
			 picPath = FileUtil.uploadFile(pic);
		}
		
		Sale sale = new Sale();
		sale.title=title;
		sale.product=targetProduct;
		sale.depict=desc;
		sale.member=member;
		sale.contract=contract;
		sale.email=email;
		sale.tel=tel;
		sale.pic=picPath;
		sale.status=status;
		sale.price=price;
		sale.save();
		
		render();
		
	}

	private static Product saveOrUpdateCompanyTypeProduct(String company, String types, String product) {
		Company sourceCompany = Company.find("name = ? ", company).first();

		Company targetCompany = null;

		if (null == sourceCompany) {
			targetCompany = new Company();
			targetCompany.name=company.trim();
			targetCompany.save();
		} else {
			targetCompany = sourceCompany;
		}

		Types sourceTypes = Types.find("name = ?", types).first();

		Types targetTypes = null;

		if (null == sourceTypes) {
			targetTypes = new Types();
			targetTypes.name=types.trim();
			targetTypes.company=targetCompany;
			targetTypes.save();
		} else {
			targetTypes = sourceTypes;
		}
		Product sourceProduct = Product.find("name = ?", product).first();

		Product targetProduct = null;

		if (null == sourceProduct) {
			targetProduct = new Product();
			targetProduct.name=product;
			targetProduct.company=targetCompany;
			targetProduct.types=targetTypes;
			targetProduct.save();
		} else {
			targetProduct = sourceProduct;
		}
		return targetProduct;
	}

	private static String parseCompleteJsonForCompany(List<Company> fetch) {
		StringBuffer json = new StringBuffer();

		json.append("{ \"list\" : [");

		for (int i = 0; i < fetch.size(); i++) {

			json.append("\"").append(fetch.get(i).name).append("\"");

			if (i < fetch.size() - 1) {

				json.append(",");
			}
		}

		json.append("]}");
		return json.toString();
	}

	private static String parseCompleteJsonForTypes(List<Types> fetch) {
		StringBuffer json = new StringBuffer();

		json.append("{ \"list\" : [");

		for (int i = 0; i < fetch.size(); i++) {

			json.append("\"").append(fetch.get(i).name).append("\"");

			if (i < fetch.size() - 1) {

				json.append(",");
			}
		}

		json.append("]}");
		return json.toString();
	}

	private static String parseCompleteJsonForProduct(List<Product> fetch) {
		StringBuffer json = new StringBuffer();

		json.append("{ \"list\" : [");

		for (int i = 0; i < fetch.size(); i++) {

			json.append("\"").append(fetch.get(i).name).append("\"");

			if (i < fetch.size() - 1) {

				json.append(",");
			}
		}

		json.append("]}");
		return json.toString();
	}
}
